package dang.action.order;

import java.io.UnsupportedEncodingException;

import dang.action.DangAction;
import dang.action.cart.Cart;
import dang.common.CookieUtil;
import dang.dao.DaoFactory;
import dang.dao.OrderDao;
import dang.entity.ReceiveAddress;
import dang.entity.User;
import dang.exception.DangException;

public class ProcessOrder extends DangAction{
	private ReceiveAddress address;
	
	public String execute() {
		User user=(User) session.get("user");
		int id = user.getId();
		address.setUser_id(id);
		OrderDao dao = DaoFactory.getOrderDao();
		try {
			if(address.getId()==-1)
				dao.saveAddress(address);
			//订单成功 完成cart
			Cart cart = (Cart) session.get("cart");
			cart.clear();
			String cartInfoId=(String) session.get("cartInfoId").toString();
			CookieUtil.saveCookie("cartInfo"+cartInfoId, cart.store(), response);
			
		} catch (DangException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "success";
	}

	public ReceiveAddress getAddress() {
		return address;
	}

	public void setAddress(ReceiveAddress address) {
		this.address = address;
	}
	
}
